//
//  RemarksViewController.m
//  GuoBuJiaForIphone
//
//  Created by zhangwo on 16/11/3.
//  Copyright © 2016年 GuoBuJia. All rights reserved.
//

#import "RemarksViewController.h"

@interface RemarksViewController ()

@end

@implementation RemarksViewController
@synthesize editRemarkView = _editRemarkView;
@synthesize photoImage = _photoImage;
@synthesize confirmBtn = _confirmBtn;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"备注"];
    [self.view setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.leftBarButtonItem = [DSXUI barButtonWithStyle:DSXBarButtonStyleBack target:self action:@selector(back)];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(8, 10, SWIDTH-16, SWIDTH)];
    bgView.backgroundColor = [UIColor colorWithHexString:@"0xEEEEEE"];
    bgView.layer.cornerRadius = 10;
    
    _editRemarkView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, SWIDTH-100)];
    _editRemarkView.backgroundColor = [UIColor colorWithHexString:@"0xEEEEEE"];
    _editRemarkView.textColor = [UIColor bgColor];
    _editRemarkView.delegate = self;
    _editRemarkView.scrollEnabled = NO;
    _editRemarkView.layer.masksToBounds = NO;
    _editRemarkView.layer.cornerRadius = 10;
    _editRemarkView.font = [UIFont systemFontOfSize:16.0f];
    _editRemarkView.textContainerInset = UIEdgeInsetsMake(10, 5, 5, 5);
    _editRemarkView.textContainerInset = UIEdgeInsetsMake(10, 5, 5, 5);
    
    //提示语句
    _placeHolder = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    _placeHolder.text = @"编辑备注";
    _placeHolder.textColor = [UIColor grayColor];
    _placeHolder.font = [UIFont systemFontOfSize:16.0f];
    [_placeHolder sizeToFit];
    [_editRemarkView addSubview:_placeHolder];
    [bgView addSubview:_editRemarkView];
    [_editRemarkView becomeFirstResponder];
    
    _photoImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, SWIDTH-80, 70, 70)];
    _photoImage.image = [UIImage imageNamed:@"icon-photo-image.png"];
    _photoImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseImage:)];
    [bgView addGestureRecognizer:singleTap];
    [bgView addSubview:_photoImage];
    
    [self.view addSubview:bgView];
    
    _confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(8, SWIDTH+100, SWIDTH-16, 40)];
    [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_confirmBtn setBackgroundColor:[UIColor colorWithHexString:@"0xDBDBDB"]];
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_confirmBtn setBackgroundColor:[UIColor colorWithHexString:@"0xFF414B"]];
    
    _confirmBtn.layer.cornerRadius = 10;
    [self.view addSubview:_confirmBtn];
    
}

- (void)back{
    if (![self.navigationController popViewControllerAnimated:YES]) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

//textView 提示语句显示与隐藏
- (void)textViewDidChange:(UITextView *)textView{
    if (self.editRemarkView.text.length > 0) {
        _placeHolder.hidden = YES;
    }else {
        _placeHolder.hidden = NO;
    }
}

- (IBAction)chooseImage:(id)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc]init];
    [sheet addButtonWithTitle:@"从相册选择"];
    [sheet addButtonWithTitle:@"拍照"];
    [sheet addButtonWithTitle:@"取消"];
    [sheet setCancelButtonIndex:2];
    [sheet setDelegate:self];
    sheet.tag = 255;
    
    [sheet showInView:self.view];
}

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        UIImagePickerController *pickController = [[UIImagePickerController alloc] init];
        pickController.delegate = self;
        if (buttonIndex == 0) {
            pickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:pickController animated:YES completion:nil];
        }
        if (buttonIndex == 1) {
            pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:pickController animated:YES completion:nil];
        }
    }
}

#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];//原图片
    // 保存图片至本地
    [self saveImage:image withName:@"currentImage.png"];
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    [self.photoImage setImage:savedImage];
    self.photoImage.tag = 100;
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
